package com.laoniu.utils;

import com.laoniu.entity.SecurityUser;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;

import java.util.Objects;

public class SpringSecurityUtils {

    public static SecurityContext getContext(){
        return SecurityContextHolder.getContext();
    }

    public static Authentication getAuthentication(){
        return getContext().getAuthentication();
    }

    public static SecurityUser getUser(){
        Object principal = getAuthentication().getPrincipal();
        if (Objects.isNull(principal)){
            return null;
        }
        return (SecurityUser) principal;
    }
}
